﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DomainModel;
using Utils;
using WebUI.Utils;

namespace WebUI.Models
{
    public class IndShotsViewModel: BaseViewModel
    {
        private int _numPerPage = 12;

        public IndShotsViewModel()
            : base()
        { }

        public IndShotsViewModel(string useName, int loginID, int page)
            : base(new { UserName = useName, LoginID =  loginID, Page = page})
        {

        }

        public Boolean IsIndAccountOwner { get; set; }
        public string AvatarUrl { get; set; }
        public Login Login { get; set; }
        public Ind Ind { get; set; }
        public List<Shot> ShotList { get; set; }
        public List<Skill> SkillList { get; set; }
        public int PreviousPage { get; set; }
        public int NextPage { get; set; }

        public Boolean IsIndSaved { get; set; }
        public Boolean IsSentRecently { get; set; }

        protected override void SetVarsWhichDependOnDynamicParams(dynamic dynParams)
        {
            //init data
            string userName = dynParams.UserName;
            int loginID = dynParams.LoginID;
            int page = dynParams.Page;

            IsIndAccountOwner = Identity.IsIndAccountOwner(userName);
            
            IndLogin indLogin = DataUtils.GetIndLoginByID(loginID);
            Login = indLogin.Login;
            Ind = indLogin.Ind;
            SkillList = DataUtils.GetList<Skill>(ConstUtils.SP_GetSkillListByID, new { IndID = Ind.IndID });
            AvatarUrl = ImageUtils.GetAvatarRelativeUrl(AvatarType.IndAvatar, Ind.IndID, AvatarSizeEnum.Avatar_80x80, Ind.AvatarFileExtension);

            //shot list
            ShotList = DataUtils.GetList<Shot>(ConstUtils.SP_GetShotsByID, new { IndID = Ind.IndID, Page = page, NumPerPage = _numPerPage });
            foreach (var shot in ShotList)
            {
                shot["ShotUrl"] = ImageUtils.GetShotRelativeUrl(Ind.IndID, shot.ShotID, ShotSizeEnum.Shot_200x133, shot.FileExtension);
            }
            //get next page
            int totalCount = ShotList != null && ShotList.Count > 0 ? ShotList[0].TotalCount : 0;
            PreviousPage = CommonUtilities.GetPreviousPage(page);
            NextPage = CommonUtilities.GetNextPage(totalCount, page, _numPerPage);

            if (Identity.IsLoggedIn)
            {
                SavedInd savedInd = DataUtils.GetOne<SavedInd>(ConstUtils.SP_GetOneSavedInd, new { SaveLoginID = Identity.LoginID, SavedLoginID = loginID});
                IsIndSaved = savedInd != null && savedInd.SavedIndID > 0;

                EmailLog emailLog = DataUtils.GetOne<EmailLog>(ConstUtils.SP_GetOneEmailLog, new { SenderLoginID = Identity.LoginID, ReceiverLoginID = loginID});
                IsSentRecently = ViewModelUtils.IsSentRecently(emailLog);
            }
            else
            {
                IsIndSaved = false;
                IsSentRecently = false;
            }
            
        }

        
    }
}